package GUI;

import Logica.Invoiced;

import java.awt.Color;
import java.awt.Dimension;

import java.awt.Font;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Factura extends JFrame {
  private JLabel jLFactura = new JLabel();
  private JButton jBAgregarFactura = new JButton();
  private JButton jBModificarFactura = new JButton();
  private JTable jTFactura = new JTable();
  private JButton jBEliminarFactura1 = new JButton();
  Invoiced inv=new Invoiced();

  public Factura() {
    try {
      jbInit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.getContentPane().setLayout( null );
    this.setSize(new Dimension(842, 527));
    this.setTitle("FACTURA");
    jLFactura.setText("FACTURA");
    jLFactura.setBounds(new Rectangle(295, 15, 170, 80));
    jLFactura.setFont(new Font("ALGERIAN", 0, 30));
    jLFactura.setForeground(new Color(0, 0, 214));
    jBAgregarFactura.setText("AGREGAR");
    jBAgregarFactura.setBounds(new Rectangle(395, 455, 125, 35));
    jBAgregarFactura.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBAgregarFactura_actionPerformed(e);
        }
      });
    jBModificarFactura.setText("MODIFICAR");
    jBModificarFactura.setBounds(new Rectangle(530, 455, 125, 35));
    jBModificarFactura.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBModificarFactura_actionPerformed(e);
        }
      });
    jBEliminarFactura1.setText("ELIMINAR");
    jBEliminarFactura1.setBounds(new Rectangle(675, 455, 125, 35));
    jBEliminarFactura1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBEliminarFactura1_actionPerformed(e);
        }
      });
    this.getContentPane().add(jBEliminarFactura1, null);
    this.getContentPane().add(jTFactura, null);
    this.getContentPane().add(jBModificarFactura, null);
    this.getContentPane().add(jBAgregarFactura, null);
    this.getContentPane().add(jLFactura, null);
    Invoiced in=new Invoiced();
    
    Object[][] info = in.DarDatosGuiFACTURA();
    String[] tit =
    { "INVOICED NUMBER", "INICIAL DATE", "EXPECTED DAY", "PAY DAY", "TOTAL",
      "STATUS", "OWNER NUMBER", "TYPE PAY" };
    
    DefaultTableModel dtm = new DefaultTableModel(info, tit);

    jTFactura = new JTable(dtm);

    jTFactura.setPreferredScrollableViewportSize(new Dimension(300,
                                                                     100)); // las dimensiones
    jTFactura.setBounds(new Rectangle(45, 85, 780, 350));
    jTFactura.setSize(new Dimension(750, 350));
    this.getContentPane().add(jTFactura, null);
  }

  private void jBAgregarFactura_actionPerformed(ActionEvent e) {
    AgregarFactura af = new AgregarFactura();
    af.setVisible(true);
  }


  private void jBModificarFactura_actionPerformed(ActionEvent e) {
    int fila = jTFactura.getSelectedRow();
    System.out.println(fila);
    Object[] info = new Object[8];
    for (int i = 0; i < jTFactura.getColumnCount(); i++){
      info[i] = jTFactura.getValueAt(fila, i);
    }
    ModificarFactura modFac = new ModificarFactura(info);
    modFac.setVisible(true);
  }


  private void jBEliminarFactura1_actionPerformed(ActionEvent e) {
    int fila = jTFactura.getSelectedRow();
    
    Object[] info = new Object[8];
    for (int i = 0; i < jTFactura.getColumnCount(); i++){
      info[i] = jTFactura.getValueAt(fila, i);
    }
    System.out.println(info[0]);
    if(inv.EliminarTratamiento((String.valueOf(info[0]))))
    {
      JOptionPane.showMessageDialog(null, "Eliminado", "El tratamiento ha sido eliminado", JOptionPane.INFORMATION_MESSAGE); 
      }
    else{
        JOptionPane.showMessageDialog(null, "No Eliminado",
                                      "El tratamiento no ha sido Eliminado",
                                      JOptionPane.ERROR_MESSAGE);
      }
    
  }
}
